#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
#define DEBUG
inline int read()
{
    int c=getchar(), f=1, x=0;
    if(c=='-') f*=-1;
    while(c<'0'&&'9'<c) c=getchar();
    while('0'<=c&&c<='9') 
        x=(x<<3)+(x<<1)+c-'0', c=getchar();
    return x;
}
inline void write(int x)
{
    if(x>=10) write(x/10);
    putchar(x%10+'0');
}

const int N=110, M=10010, INF=0x3f3f3f3f;
int n,m;
int h[N], e[M], ne[M], w[M], idx;
// int rh[N], re[M], rne[M], rw[M], ridx;
int dur[N], early[N], last[N]; //最早开始日期 最晚开始日期
bool st[N];
int maxend;
inline void AddEdge(int a, int b)
{
    e[idx]=b, ne[idx]=h[a], h[a]=idx++; 
}

void init()
{
    memset(h, -1, sizeof h);
    // memset(rh, -1, sizeof rh);
    memset(early, 0, sizeof early);
    memset(last, INF, sizeof last);
    
    n=read(), m=read();
    for(int i=1; i<=m; i++)
    {
        int fa=read();
        AddEdge(fa, i);
    }
    dur[0]=1;
    for(int i=1; i<=m; i++)
        dur[i]=read();
}

void dfs1(int u) 
{
    st[u]=true;
    for(int i=h[u]; ~i; i=ne[i])
    {
        int v=e[i];
        early[v]=max(early[v], early[u]+dur[u]); //这里不需要-1，因为v第二天才能开始，需要+1
        maxend=max(maxend, early[v]+dur[v]-1);
        if(!st[v]) dfs1(v);
        //printf("%d->%d early[%d]:max(%d, %d+%d)\n", u, v, v, early[v], early[u], dur[u]);
    }
}

void dfs2(int u)
{
    st[u]=true;
    bool leaf=true;
    //先判断是否为叶子节点
    for(int i=h[u]; ~i; i=ne[i])
    {
        int v=e[i];
        if(!st[v])
        {
            dfs2(v);
            leaf=false;
        }
    }

    //叶子节点直接初始化
    if(leaf) last[u]=n-dur[u]+1;
    else
    {
        for(int i=h[u]; ~i; i=ne[i])
        {
            int v=e[i];
            last[u]=min(last[u], last[v]-dur[u]);
        }
    }
}

void solve()
{
    init();

    memset(st, 0, sizeof st);
    for(int i=1; i<=m; i++)
        if(!st[i])
            dfs1(0); //深搜每一个活动

    for(int i=1; i<=m; i++)
        write(early[i]), putchar(' ');
    puts("");

    if(maxend>n) return;

    memset(st, 0, sizeof st);
    
    for(int i=1; i<=m; i++)
        if(!st[i])
            dfs2(0); //深搜每一个活动
    for(int i=1; i<=m; i++)
        write(last[i]), putchar(' ');
    puts("");
}

int main()
{
    #ifdef DEBUG
        freopen("../in.txt", "r", stdin);
        freopen("../out.txt", "w", stdout);
    #endif

    int T=1; //cin >> T; 
    while(T--) 
    {
        solve();
    }
    return 0;
}